package com.ruoyi.web.controller.farmer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.farmer.domain.Spu;
import com.ruoyi.farmer.service.ISpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品管理Controller
 *
 * @author 姚毫征
 * @date 2023-09-13
 */
@RestController
@RequestMapping("/spu/spu")
public class SpuController extends BaseController
{
    @Autowired
    private ISpuService spuService;

    /**
     * 查询产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('spu:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(Spu spu)
    {
        startPage();
        List<Spu> list = spuService.selectSpuList(spu);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('spu:spu:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spu spu)
    {
        List<Spu> list = spuService.selectSpuList(spu);
        ExcelUtil<Spu> util = new ExcelUtil<Spu>(Spu.class);
        util.exportExcel(response, list, "产品管理数据");
    }

    /**
     * 获取产品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('spu:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spuService.selectSpuById(id));
    }

    /**
     * 新增产品管理
     */
    @PreAuthorize("@ss.hasPermi('spu:spu:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Spu spu)
    {
        spu.setSpuId(UUID.randomUUID().toString());
        spu.setCreateTime(DateUtils.getTime());
        return toAjax(spuService.insertSpu(spu));
    }
    @PreAuthorize("@ss.hasPermi('spu:spu:getSpuByVariety')")
    @GetMapping(value = "/getSpuByVariety/{id}")
    public AjaxResult getSpuByVariety(@PathVariable("id") String varietyId)
    {
        return success(spuService.getSpuByVariety(varietyId));
    }

    /**
     * 修改产品管理
     */
    @PreAuthorize("@ss.hasPermi('spu:spu:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Spu spu)
    {
        spu.setUpdateTime(DateUtils.getTime());
        return toAjax(spuService.updateSpu(spu));
    }

    /**
     * 删除产品管理
     */
    @PreAuthorize("@ss.hasPermi('spu:spu:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spuService.deleteSpuByIds(ids));
    }
}
